<template>
  <div id="concourse">  

      <el-form ref="form"  label-width="80px">           
           <el-row>
            <el-col :span="12" >  
               <div style="height:38px;line-height:38px;width:150px;"> 
                  <i class="el-icon-back" @click="goBack"></i> 
                  {{ $route.query.name }} 
                </div>
            </el-col>
            <el-col :span="12" style="text-align: right;margin-bottom:10px;">
               <el-button class="custom_color_button" type="primary" @click="add_but()">新增内容</el-button>
            </el-col>
          </el-row>
    </el-form>

   <!-- 渲染数据list -->
   <con-course-list :datalist="datalist" @up_list_fun="list_fun"> </con-course-list>

      
   <div style="margin-top:20px;text-align: center;">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :page-size="10"  
        layout="prev, pager, next, jumper"
        :total="count">
      </el-pagination>
    </div>


     <el-dialog title="添加信息" :visible.sync="add_infor_mtk"  width="800px" >
          <el-form ref="form" :model="formlist"  label-width="100px" style="width:100%;">

             <el-form-item label="内容标题">
                    <el-input    v-model="formlist.title" placeholder="中文标题"></el-input>
              </el-form-item> 

             <!-- 富文本选取图片 -->
                <el-form-item label="详情" style="">
                    <div style="">
                        <quill-editor class="editor"
                            v-model="formlist.content" 
                            ref="myQuillEditor" 
                            :options="editorOption" 
                            @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
                            @change="onEditorChange($event)" style="height:300px;margin-bottom:100px;">
                        </quill-editor>
                    </div>
                </el-form-item>
            <!-- 富文本选取图片 -->
            <el-form-item label="" v-show="false" >  
                <el-upload                                                
                    action="uploadUrl"  
                    :show-file-list="false"   
                    :before-upload="beforeupload"
                    :http-request="ImgUploadSectionFile">
                        <el-button slot="trigger" size="small" type="primary" id="selectImg">选取文件</el-button>
                </el-upload>
            </el-form-item>


            

         </el-form>
          <div style="text-align: center;width:80%;margin:auto;">
                  <el-button type="primary" style="width:80%;" class="custom_color_button" @click="add_infor_but_qr">确认</el-button>
          </div>

   </el-dialog>  

      
  </div>
</template>

<script>
import toolbarOptions from '@/assets/js/quillConfig.js'
import ConCourseList from '@/view/Course/ConCourseList'
export default {
  name: 'concourse',
  data () {
    return {
        datalist:[],
        page:1,
        count:0,
        my_study_id:'',
        add_infor_mtk:false,
        formlist:{
            content:'',
            upfile:'',
            title:''
        },
        editorOption: {//富文本
          modules: {
            toolbar: {
              container: toolbarOptions,  // 工具栏
              handlers: {
                'image': function (value) {
                  if (value) {
                    // alert('自定义图片')
                    document.getElementById("selectImg").click();    //调用选择图片
                    
                  } else {
                   // this.quill.format('image', false);
                  }
                }
              }
            }
           }       
        },
    }
  },
 components:{
    ConCourseList
  },
  created(){
      this.my_study_id = this.$route.query.id
      this.list_fun()
  },
  mounted (){
    
  },
  methods: {
      list_fun(){
          this.post("/study_guide/listOfContent", {
              page:this.page,
              stid:this.my_study_id,
	        }).then(res => {
            this.count = res.result.count
            this.datalist = res.result.list

            // this.datalist.forEach(function(value,index,array){
            //    array[index].index = index+1

            // });
            //console.log(res)
         })
      },
      add_but(){
          this.add_infor_mtk = true
      },
      add_infor_but_qr(){

           this.post("/study_guide/addContent", {
                   stid:this.my_study_id,
                   content:this.formlist.content,
                   title:this.formlist.title
                }).then(res => {

                    if(res.error_code == 0){
                        this.open1(res.result,'success')  //提示框
                        this.add_infor_mtk = false
                        this.formlist.content=''
                        this.formlist.title = ''
                        this.list_fun()

                    }else {
                     this.open1(res.message,'warning')
                    }
               })

          console.log(this.formlist)
      },
      ImgUploadSectionFile(param){//图片上传    
      },
      beforeupload(file){   //重新写入上穿 需要点击确认上传与表单一起提交
           if(this.upimglx(file) == false){  //判断是否为fales
              this.upimglxtitle()  //执行弹出错误             
              return false
            }           
            
            let that = this
            this.upimgpost("/image/uploadMultiple",file).then(res => {
              this.formlist.upfile = res.result.url   
              console.log(this.formlist.upfile)
              this.formlist.content = this.formlist.content +'<img src="'+  this.urlimg + this.formlist.upfile +'"/>'
           })

          return false;
      },
        onEditorBlur(){ //富文本
        },
        onEditorFocus(){//富文本获得焦点事件
        },
        onEditorChange(){//富文本内容改变事件
        //console.log(this.xg_school.content)
        },
      goBack() {
        this.$router.go(-1)
     },
     handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.page = val
        this.list_fun()
        console.log(`当前页: ${val}`);
      }
  }
}
</script>


<style scoped>
</style>
